<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<jsp:include page="../common/EditUIHead.jsp"></jsp:include>
	<title>餐馆编辑</title>
</head>
<body>
	<div>
		<form id="dataForm" action="${base }/meal/restaurant/save" method="post">
	  		<input type="hidden" id="dataId" name="id" value="${data.id}"/>
	  		<input type="hidden" id="entryJson" name="entryJson" />
	    	<div>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
	                    <td width="60px"><span style="color:red">*</span>名称：</td>
	                    <td><input name="name" id="name" validate="{required:true,maxlength:20}" type="text" value="${data.name}"/></td>
	                    <td width="60px">QQ：</td>
	                    <td><input name="qq" id="qq" validate="{maxlength:15}" type="text" value="${data.qq}"/></td>
	                </tr>
	                <tr>
	                    <td width="60px"><span style="color:red">*</span>电话：</td>
	                    <td colspan="3"><input name="phone" id="phone" validate="{required:true,maxlength:15}" type="text" value="${data.phone}"/></td>
	                </tr>
	                <tr>
	                    <td width="60px">地址：</td>
	                    <td colspan="3"><input name="addr" id="addr" validate="{maxlength:100}" type="text" value="${data.addr}"/></td>
	                </tr>
	                <tr>
	                    <td width="60px">网址：</td>
	                    <td colspan="3"><input name="web" id="web" validate="{maxlength:100}" type="text" value="${data.web}"/></td>
	                </tr>
				</table>
			</div>
		</form>
		<div id="entryContainer">
	    </div>    
	</div>
</body>
</html>
<script type="text/javascript">
	var entryGrid;
	var entryData = eval('('+'${entryData}'+')');
	$(document).ready(function(){
		entryGrid=$("#entryContainer").ligerGrid({
			toolbar: { items: [
                {id:'addNewRow',text:'增加',click:addEntry,icon:'add'},
                {line: true },
                {text: '删除', click: removeEntry, icon: 'delete'}
                ]
                },
			columns:[
				{display:'菜名',name:'name',width:350,align:'left',editor:{type:'text'}},
				{display:'单价',name:'price',width:80,align:'left',editor:{type:'float',minValue:0}}
				],
			width:'99%',
			enabledSort:false,
			height:'300px',
			usePager:false,
			alternatingRow:false,
			checkbox:false,
			enabledEdit:$edit_viewstate!='VIEW',
			data:entryData,
			rownumbers:true,
			//以下用于兼容后台的pagination
		    root:'dishes',
		    onBeforeSubmitEdit:function(e){
		    	//e(columnname、columnindex、column、reocrd、value)(单单元格编辑状态)或者e(reocrd、rowindex、newdata)
		    	if(e.column.columnname=='price'){
		    		if(isNaN(e.value) || e.value=='' || parseInt(e.value) < 0){
			    		e.value = null;
			    		return false;
			    	}else{
			    		e.value = parseInt(e.value);
			    		return true;
			    	}
		    	}
		    }
		});
	});
	function addEntry(){
		entryGrid.addEditRow({});
	}
	function removeEntry(){
		entryGrid.deleteSelectedRow();
	}
	function saveEdit(){
		var datas = entryGrid.getData();
		if(datas && datas.length > 0){
			var str = "[";
			for(var i = 0; i < datas.length;i++){
					if(datas[i].id && datas[i].id!='undefined'){
						str += "{id:'"+datas[i].id+"'";
					}else{
						str +="{id:''";
					}
					if(datas[i].name&& datas[i].name!='undefined'){
						str +=",name:'"+datas[i].name+"'";
					}else{
						art.dialog.tips("第"+(i+1)+"行菜名为空");
						return;
					}
					if(!isNaN(datas[i].price&& datas[i].price!='undefined')){
						str +=",price:'"+datas[i].price+"'}";
					}else{
						art.dialog.tips("第"+(i+1)+"行单价不合法");
						return;
					}
					if(i != datas.length-1){
						str += ",";
					}
			}
			str += "]";
			$('#entryJson').val(str);
		}
		/* $.post('/shihua-hr/meal/restaurant/save',$('#dataForm').serialize()+'&entryJson='+encodeURIComponent(str),function(res){
			art.dialog.close();
		},'json') */;
		$("form").submit();
		return false;
	}
	function saveAdd(){
		saveEdit();
	}
</script>